package pw.canyingisme.plugin.config;

import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffectType;
import pw.canyingisme.plugin.api.Config;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

public class ConfigLoader {
    private static FileConfiguration OccupationConfig;
    private static FileConfiguration PlayerOccupationConfig;
    private static FileConfiguration ActionPackConfig;

    public static boolean loadOccupationConfig(JavaPlugin plugin){
       Config.saveDefaultConfig(plugin,"Occupation.yml");
       OccupationConfig= Config.LoadYamlConfiguration(plugin,"Occupation.yml");
       return !(OccupationConfig == null);
    }
    public static Set<String> getOccupationSet(){
        return OccupationConfig.getKeys(false);
    }
    public static Iterator setGetIterator(Set<String> set){
        Iterator it = set.iterator();
        return it;
    }
    public static boolean loadPlayerOccupationConfig(JavaPlugin plugin){
        Config.saveDefaultConfig(plugin,"player.yml");
        PlayerOccupationConfig= Config.LoadYamlConfiguration(plugin,"player.yml");
        return !(PlayerOccupationConfig == null);
    }
    public static String occupationGetName(String occupation){
        return OccupationConfig.getString(occupation+".name");
    }
    public static void setPlayerOccupation(String playerName,String Occupation) throws IOException {
        PlayerOccupationConfig.set("occupation."+playerName+".occupation",Occupation);
        PlayerOccupationConfig.save("./plugins/ShadowOccupation/player.yml");
    }
    public static boolean hasOccupation(String playerName){
        if(PlayerOccupationConfig.getString("occupation."+playerName+".occupation").equalsIgnoreCase("")){
            return false;
        }
        if (PlayerOccupationConfig.getString("occupation."+playerName+".occupation").equalsIgnoreCase("false")){
            return false;
        }
        return true;
    }
    public static String getPlayerOccupation(String playerName){
        return PlayerOccupationConfig.getString("occupation."+playerName+".occupation");
    }
    public static boolean removePlayerOccupation(CommandSender sender, String playerName){
        PlayerOccupationConfig.set("occupation."+playerName+".occupation","false");
        try {
            PlayerOccupationConfig.save("./plugins/ShadowOccupation/player.yml");
            return true;
        } catch (IOException e) {
            sender.sendMessage("为玩家"+playerName+"移除职业失败 原因:"+e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    public static Set<String> getOccupationEffectList(String Occupation) {
        return OccupationConfig.getConfigurationSection(Occupation+".effect").getKeys(false);
    }
    public static int getOccupationEffectLevel(String Occupation,String Effect){
        return OccupationConfig.getInt(Occupation+".effect."+Effect+".level");
    }
    public static PotionEffectType effectGetMCEffect(String Occupation,String Effect){
        String ConfigEffect= OccupationConfig.getString(Occupation+".effect."+Effect+".Effect");
        return PotionEffectType.getByName(ConfigEffect);
    }
    public static boolean LoadActionConfig(JavaPlugin plugin){
        Config.saveDefaultConfig(plugin,"ActionPack.yml");
        ActionPackConfig= Config.LoadYamlConfiguration(plugin,"ActionPack.yml");
        return ActionPackConfig != null;
    }
    public static FileConfiguration getActionConfig(){
        return ActionPackConfig;
    }
    public static String getOccupationActionPack(String Occupation,String Event){
        return OccupationConfig.getString(Occupation+".Action."+Event+".ActionPack.ActionName");
    }
    public static int getOccupationActionPackPercent(String Occupation,String Event){
        return OccupationConfig.getInt(Occupation+".Action."+Event+".ActionPack.ActionPercent");
    }
    public static boolean hasOccupationActionPackPercent(String Occupation,String Event){
        return OccupationConfig.getInt(Occupation+".Action."+Event+".ActionPack.ActionPercent")==0;
    }
    public static boolean hasActionPack(String Occupation,String Event){
        if(OccupationConfig.getString(Occupation+".Action."+Event+".ActionPack.ActionName")==null){
            return false;
        }if(OccupationConfig.getString(Occupation+".Action."+Event+".ActionPack.ActionName").equalsIgnoreCase("")){
            return false;
        }
        return true;
    }
    public static double getOccupationHealth(String Occupation){
        return OccupationConfig.getDouble(Occupation+".Health");
    }
    public static boolean hasOccupationHealth(String Occupation){
        if(OccupationConfig.getDouble(Occupation+".Health")==0){
            return false;
        }return true;
    }
}
